WechatUser.php 7.9 KB

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