WechatUser.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/12/21
  6. */
  7. namespace app\models\user;
  8. use app\models\store\StoreCouponIssue;
  9. use crmeb\basic\BaseModel;
  10. use crmeb\services\WechatService;
  11. use crmeb\traits\ModelTrait;
  12. use crmeb\services\SystemConfigService;
  13. use app\models\routine\RoutineQrcode;
  14. use app\models\store\StoreCouponUser;
  15. /**
  16. * TODO 微信用户Model
  17. * Class WechatUser
  18. * @package app\models\user
  19. */
  20. class WechatUser extends BaseModel
  21. {
  22. /**
  23. * 数据表主键
  24. * @var string
  25. */
  26. protected $pk = 'uid';
  27. /**
  28. * 模型名称
  29. * @var string
  30. */
  31. protected $name = 'wechat_user';
  32. use ModelTrait;
  33. /**
  34. * uid获取小程序Openid
  35. * @param string $uid
  36. * @return bool|mixed
  37. */
  38. public static function getOpenId($uid = '')
  39. {
  40. if ($uid == '') return false;
  41. return self::where('uid', $uid)->value('routine_openid');
  42. }
  43. /**
  44. * TODO 用uid获得openid
  45. * @param $uid
  46. * @param string $openidType
  47. * @return mixed
  48. * @throws \Exception
  49. */
  50. public static function uidToOpenid($uid, $openidType = 'routine_openid')
  51. {
  52. $openid = self::where('uid', $uid)->value($openidType);
  53. return $openid;
  54. }
  55. /**
  56. * TODO 用openid获得uid
  57. * @param $openid
  58. * @param string $openidType
  59. * @return mixed
  60. */
  61. public static function openidTouid($openid, $openidType = 'openid')
  62. {
  63. return self::where($openidType, $openid)->where('user_type', '<>', 'h5')->value('uid');
  64. }
  65. /**
  66. * 小程序创建用户后返回uid
  67. * @param $routineInfo
  68. * @return mixed
  69. */
  70. public static function routineOauth($routine)
  71. {
  72. $routineInfo['nickname'] = filter_emoji($routine['nickName']);//姓名
  73. $routineInfo['sex'] = $routine['gender'];//性别
  74. $routineInfo['language'] = $routine['language'];//语言
  75. $routineInfo['city'] = $routine['city'];//城市
  76. $routineInfo['province'] = $routine['province'];//省份
  77. $routineInfo['country'] = $routine['country'];//国家
  78. $routineInfo['headimgurl'] = $routine['avatarUrl'];//头像
  79. $routineInfo['routine_openid'] = $routine['openId'];//openid
  80. $routineInfo['session_key'] = $routine['session_key'];//会话密匙
  81. $routineInfo['unionid'] = $routine['unionId'];//用户在开放平台的唯一标识符
  82. $routineInfo['user_type'] = 'routine';//用户类型
  83. $spid = 0;//绑定关系uid
  84. $isCOde = false;
  85. //获取是否有扫码进小程序
  86. if ($routine['code']) {
  87. if ($info = RoutineQrcode::getRoutineQrcodeFindType($routine['code'])) {
  88. $spid = $info['third_id'];
  89. $isCOde = true;
  90. } else
  91. $spid = $routine['spid'];
  92. } else if ($routine['spid'])
  93. $spid = $routine['spid'];
  94. // 判断unionid 存在根据unionid判断
  95. if ($routineInfo['unionid'] != '' && ($uid = self::where(['unionid' => $routineInfo['unionid']])->where('user_type', '<>', 'h5')->value('uid'))) {
  96. self::edit($routineInfo, $uid, 'uid');
  97. $routineInfo['code'] = $spid;
  98. $routineInfo['isPromoter'] = $isCOde;
  99. if ($routine['login_type']) $routineInfo['login_type'] = $routine['login_type'];
  100. User::updateWechatUser($routineInfo, $uid);
  101. } else if ($uid = self::where(['routine_openid' => $routineInfo['routine_openid']])->where('user_type', '<>', 'h5')->value('uid')) { //根据小程序openid判断
  102. self::edit($routineInfo, $uid, 'uid');
  103. $routineInfo['code'] = $spid;
  104. $routineInfo['isPromoter'] = $isCOde;
  105. if ($routine['login_type']) $routineInfo['login_type'] = $routine['login_type'];
  106. User::updateWechatUser($routineInfo, $uid);
  107. } else {
  108. $routineInfo['add_time'] = time();//用户添加时间
  109. $routineInfo = self::create($routineInfo);
  110. $res = User::setRoutineUser($routineInfo, $spid);
  111. $uid = $res->uid;
  112. }
  113. return $uid;
  114. }
  115. /**
  116. * 判断是否是小程序用户
  117. * @param int $uid
  118. * @return bool|int|string
  119. */
  120. public static function isRoutineUser($uid = 0)
  121. {
  122. if (!$uid) return false;
  123. return self::where('uid', $uid)->where('user_type', 'routine')->count();
  124. }
  125. /**
  126. * @param int $uid
  127. * @return int
  128. */
  129. public static function isUserStatus($uid = 0)
  130. {
  131. if (!$uid) return 0;
  132. $user = User::getUserInfo($uid);
  133. return $user['status'];
  134. }
  135. /**
  136. * 检查数字是否包含4
  137. * @param $number
  138. * @return bool
  139. */
  140. protected static function hasDigitFour($number)
  141. {
  142. return strpos((string)$number, '4') !== false;
  143. }
  144. /**
  145. * 生成不包含数字4的下一个uid
  146. * @param int|null $startUid
  147. * @return int
  148. */
  149. protected static function getNextUidWithoutFour($startUid = null)
  150. {
  151. if ($startUid === null) {
  152. $startUid = self::max('uid') ?: 0;
  153. }
  154. $uid = $startUid + 1;
  155. while (self::hasDigitFour($uid)) {
  156. $uid++;
  157. if ($uid - $startUid > 1000) {
  158. return $startUid + 1;
  159. }
  160. }
  161. return $uid;
  162. }
  163. /**
  164. * .添加新用户
  165. * @param $openid
  166. * @return object
  167. */
  168. public static function setNewUser($openid)
  169. {
  170. $userInfo = WechatService::getUserInfo($openid);
  171. if (!isset($userInfo['subscribe']) || !$userInfo['subscribe'] || !isset($userInfo['openid']))
  172. exception('请关注公众号!');
  173. $userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
  174. //判断 unionid 是否存在
  175. if (isset($userInfo['unionid'])) {
  176. $wechatInfo = self::where('unionid', $userInfo['unionid'])->find();
  177. unset($userInfo['qr_scene'],$userInfo['qr_scene_str'],$userInfo['qr_scene_str'],$userInfo['subscribe_scene']);
  178. if ($wechatInfo) {
  179. return self::edit($userInfo->toArray(), $userInfo['unionid'], 'unionid');
  180. }
  181. }
  182. self::beginTrans();
  183. $userInfo['uid'] = self::getNextUidWithoutFour();
  184. $wechatUser = self::create(is_object($userInfo) ? $userInfo->toArray() : $userInfo);
  185. if (!$wechatUser) {
  186. self::rollbackTrans();
  187. exception('用户储存失败!');
  188. }
  189. if (!User::setWechatUser($wechatUser)) {
  190. self::rollbackTrans();
  191. exception('用户信息储存失败!');
  192. }
  193. self::commitTrans();
  194. self::userFirstSubGiveCoupon($openid);
  195. return $wechatUser;
  196. }
  197. /**
  198. * TODO 关注送优惠券
  199. * @param $openid
  200. */
  201. public static function userFirstSubGiveCoupon($openid)
  202. {
  203. $couponIds = StoreCouponIssue::where('status',1)
  204. ->where('is_give_subscribe',1)
  205. ->where('is_del',0)
  206. ->column('cid');
  207. if ($couponIds)
  208. foreach ($couponIds as $couponId)
  209. if ($couponId)
  210. StoreCouponUser::addUserCoupon(self::openidToUid($openid), $couponId);
  211. }
  212. /**
  213. * 订单金额达到预设金额赠送优惠卷
  214. * @param $uid
  215. */
  216. public static function userTakeOrderGiveCoupon($uid,$total_price)
  217. {
  218. $couponIds = StoreCouponIssue::where('status',1)
  219. ->where('is_full_give',1)
  220. ->where('is_del',0)
  221. ->column('cid,full_reduction');
  222. if ($couponIds)
  223. foreach ($couponIds as $couponId)
  224. if ($couponId)
  225. if ($total_price >= $couponId['full_reduction'])
  226. StoreCouponUser::addUserCoupon($uid, $couponId['cid']);
  227. }
  228. /**
  229. * 更新用户信息
  230. * @param $openid
  231. * @return bool
  232. */
  233. public static function updateUser($openid)
  234. {
  235. $userInfo = WechatService::getUserInfo($openid);
  236. $userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
  237. return self::edit($userInfo->toArray(), $openid, 'openid');
  238. }
  239. /**
  240. * 用户存在就更新 不存在就添加
  241. * @param $openid
  242. */
  243. public static function saveUser($openid)
  244. {
  245. self::be($openid, 'openid') == true ? self::updateUser($openid) : self::setNewUser($openid);
  246. }
  247. /**
  248. * 用户取消关注
  249. * @param $openid
  250. * @return bool
  251. */
  252. public static function unSubscribe($openid)
  253. {
  254. return self::edit(['subscribe' => 0], $openid, 'openid');
  255. }
  256. /**
  257. * TODO 用uid获得Unionid
  258. * @param $uid
  259. * @return mixed
  260. */
  261. public static function uidToUnionid($uid)
  262. {
  263. return self::where('uid', $uid)->value('unionid');
  264. }
  265. /**
  266. * TODO 获取用户信息
  267. * @param $openid
  268. * @param $openidType
  269. * @return array
  270. * @throws \think\db\exception\DataNotFoundException
  271. * @throws \think\db\exception\ModelNotFoundException
  272. * @throws \think\exception\DbException
  273. */
  274. public static function getWechatInfo($openid, $openidType)
  275. {
  276. if (is_numeric($openid)) $openid = self::uidToOpenid($openid);
  277. $wechatInfo = self::where($openidType, $openid)->find();
  278. if (!$wechatInfo) {
  279. self::setNewUser($openid);
  280. $wechatInfo = self::where($openidType, $openid)->find();
  281. }
  282. if (!$wechatInfo) exception('获取用户信息失败!');
  283. return $wechatInfo->toArray();
  284. }
  285. }