WechatUser.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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)->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. * .添加新用户
  137. * @param $openid
  138. * @return object
  139. */
  140. public static function setNewUser($openid)
  141. {
  142. $userInfo = WechatService::getUserInfo($openid);
  143. if (!isset($userInfo['subscribe']) || !$userInfo['subscribe'] || !isset($userInfo['openid']))
  144. exception('请关注公众号!');
  145. $userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
  146. //判断 unionid 是否存在
  147. if (isset($userInfo['unionid'])) {
  148. $wechatInfo = self::where('unionid', $userInfo['unionid'])->find();
  149. unset($userInfo['qr_scene'],$userInfo['qr_scene_str'],$userInfo['qr_scene_str'],$userInfo['subscribe_scene']);
  150. if ($wechatInfo) {
  151. return self::edit($userInfo->toArray(), $userInfo['unionid'], 'unionid');
  152. }
  153. }
  154. self::beginTrans();
  155. $wechatUser = self::create(is_object($userInfo) ? $userInfo->toArray() : $userInfo);
  156. if (!$wechatUser) {
  157. self::rollbackTrans();
  158. exception('用户储存失败!');
  159. }
  160. if (!User::setWechatUser($wechatUser)) {
  161. self::rollbackTrans();
  162. exception('用户信息储存失败!');
  163. }
  164. self::commitTrans();
  165. self::userFirstSubGiveCoupon($openid);
  166. return $wechatUser;
  167. }
  168. /**
  169. * TODO 关注送优惠券
  170. * @param $openid
  171. */
  172. public static function userFirstSubGiveCoupon($openid)
  173. {
  174. $couponIds = StoreCouponIssue::where('status',1)
  175. ->where('is_give_subscribe',1)
  176. ->where('is_del',0)
  177. ->column('cid');
  178. if ($couponIds)
  179. foreach ($couponIds as $couponId)
  180. if ($couponId)
  181. StoreCouponUser::addUserCoupon(self::openidToUid($openid), $couponId);
  182. }
  183. /**
  184. * 订单金额达到预设金额赠送优惠卷
  185. * @param $uid
  186. */
  187. public static function userTakeOrderGiveCoupon($uid,$total_price)
  188. {
  189. $couponIds = StoreCouponIssue::where('status',1)
  190. ->where('is_full_give',1)
  191. ->where('is_del',0)
  192. ->column('cid,full_reduction');
  193. if ($couponIds)
  194. foreach ($couponIds as $couponId)
  195. if ($couponId)
  196. if ($total_price >= $couponId['full_reduction'])
  197. StoreCouponUser::addUserCoupon($uid, $couponId['cid']);
  198. }
  199. /**
  200. * 更新用户信息
  201. * @param $openid
  202. * @return bool
  203. */
  204. public static function updateUser($openid)
  205. {
  206. $userInfo = WechatService::getUserInfo($openid);
  207. $userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
  208. return self::edit($userInfo->toArray(), $openid, 'openid');
  209. }
  210. /**
  211. * 用户存在就更新 不存在就添加
  212. * @param $openid
  213. */
  214. public static function saveUser($openid)
  215. {
  216. self::be($openid, 'openid') == true ? self::updateUser($openid) : self::setNewUser($openid);
  217. }
  218. /**
  219. * 用户取消关注
  220. * @param $openid
  221. * @return bool
  222. */
  223. public static function unSubscribe($openid)
  224. {
  225. return self::edit(['subscribe' => 0], $openid, 'openid');
  226. }
  227. /**
  228. * TODO 用uid获得Unionid
  229. * @param $uid
  230. * @return mixed
  231. */
  232. public static function uidToUnionid($uid)
  233. {
  234. return self::where('uid', $uid)->value('unionid');
  235. }
  236. /**
  237. * TODO 获取用户信息
  238. * @param $openid
  239. * @param $openidType
  240. * @return array
  241. * @throws \think\db\exception\DataNotFoundException
  242. * @throws \think\db\exception\ModelNotFoundException
  243. * @throws \think\exception\DbException
  244. */
  245. public static function getWechatInfo($openid, $openidType)
  246. {
  247. if (is_numeric($openid)) $openid = self::uidToOpenid($openid);
  248. $wechatInfo = self::where($openidType, $openid)->find();
  249. if (!$wechatInfo) {
  250. self::setNewUser($openid);
  251. $wechatInfo = self::where($openidType, $openid)->find();
  252. }
  253. if (!$wechatInfo) exception('获取用户信息失败!');
  254. return $wechatInfo->toArray();
  255. }
  256. }