WechatUser.php 8.0 KB

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