UserController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. namespace mall\controllers;
  3. use bizHd\custom\classes\CustomClass;
  4. use bizHd\wx\classes\WxOpenClass;
  5. use bizMall\message\services\SmsService;
  6. use bizMall\user\classes\UserClass;
  7. use bizMall\user\services\UserAssetService;
  8. use bizMall\user\services\UserService;
  9. use common\components\dict;
  10. use common\components\imgUtil;
  11. use common\components\jwt;
  12. use common\components\stringUtil;
  13. use Yii;
  14. use common\components\util;
  15. use yii\helpers\Json;
  16. use Overtrue\ChineseCalendar\Calendar;
  17. class UserController extends BaseController
  18. {
  19. //二维码收款使用
  20. public $guestAccess = ['discount', 'mini-mobile', 'create', 'account-login'];
  21. //清空登录信息 ssh 20250215
  22. public function actionClearLogin()
  23. {
  24. $user = $this->user;
  25. $user->miniOpenId = '';
  26. $user->save();
  27. util::complete();
  28. }
  29. //修改信息 ssh 20221015
  30. public function actionUpdate()
  31. {
  32. $post = Yii::$app->request->post();
  33. $user = $this->user;
  34. if (empty($user)) {
  35. util::fail('请先登录');
  36. }
  37. $userId = $user->id ?? 0;
  38. $address = $post['address'] ?? '';
  39. $floor = $post['floor'] ?? '';
  40. $fullAddress = $address . $floor;
  41. $post['fullAddress'] = $fullAddress;
  42. UserClass::updateUserInfo($user, $post);
  43. util::complete();
  44. }
  45. //修改生日 ssh 20250830
  46. public function actionModifyBirthday()
  47. {
  48. date_default_timezone_set('PRC');
  49. $get = Yii::$app->request->get();
  50. $lunar = $get['lunar'] ?? 0;
  51. $birthdayMonth = $get['birthdayMonth'] ?? 1;
  52. if ($birthdayMonth > 12 || $birthdayMonth < 1) {
  53. util::fail('月份必须1到12月');
  54. }
  55. $birthdayDate = $get['birthdayDate'] ?? 1;
  56. if ($birthdayDate > 31 || $birthdayDate < 1) {
  57. util::fail('日期必须1到31号');
  58. }
  59. if ($lunar == 1 && $birthdayDate == 31) {
  60. util::fail('农历没有31号');
  61. }
  62. $user = $this->user;
  63. if ($user->birthdaySet == 1) {
  64. util::fail('你已经设置过了');
  65. }
  66. $calendar = new Calendar();
  67. if ($lunar == 1) {
  68. try {
  69. $result = $calendar->lunar(date("Y"), $birthdayMonth, $birthdayDate);
  70. } catch (\InvalidArgumentException $e) {
  71. $msg = $e->getMessage();
  72. util::fail($msg);
  73. }
  74. $month = $result['gregorian_month'] ?? 1;
  75. $date = $result['gregorian_day'] ?? 1;
  76. $birthday = date("Y") . '-' . $month . '-' . $date;
  77. } else {
  78. $birthday = date("Y") . '-' . $birthdayMonth . '-' . $birthdayDate;
  79. }
  80. $birthdayTime = strtotime($birthday);
  81. $user->lunar = $lunar;
  82. $user->birthdayDate = $birthdayDate;
  83. $user->birthdayMonth = $birthdayMonth;
  84. $user->birthday = $birthday;
  85. $user->birthdayTime = $birthdayTime;
  86. $user->birthdaySet = 1;
  87. $user->save();
  88. $userId = $user->id;
  89. $customList = \bizMall\custom\classes\CustomClass::getAllByCondition(['userId' => $userId], null, '*', null, true);
  90. if (!empty($customList)) {
  91. foreach ($customList as $custom) {
  92. $custom->lunar = $lunar;
  93. $custom->birthdayDate = $birthdayDate;
  94. $custom->birthdayMonth = $birthdayMonth;
  95. $custom->birthday = $birthday;
  96. $custom->birthdayTime = $birthdayTime;
  97. $custom->save();
  98. }
  99. }
  100. util::complete('修改成功');
  101. }
  102. //获取当前登录用户的信息 ssh 20221014
  103. public function actionCurrentInfo()
  104. {
  105. $user = $this->user;
  106. if (!empty($user)) {
  107. // 将 $user 转换为数组,然后添加 hasMap 属性(字典中的 hasMap)
  108. $user = $user->toArray();
  109. $shortAvatar = $user['avatar'] ?? '';
  110. $smallAvatar = imgUtil::groupImg($shortAvatar) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  111. $avatar = imgUtil::groupImg($shortAvatar) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  112. $user['avatar'] = $avatar;
  113. $user['smallAvatar'] = $smallAvatar;
  114. $user['shortAvatar'] = $shortAvatar;
  115. }
  116. $hasMap = dict::getDict('hasMap');
  117. $user['hasMap'] = $hasMap;
  118. util::success(['info' => $user]);
  119. }
  120. //使用账号登录 ssh 20220920
  121. public function actionAccountLogin()
  122. {
  123. $post = Yii::$app->request->post();
  124. $mobile = $post['mobile'] ?? '';
  125. $password = $post['password'] ?? '';
  126. if ($password != 'a.1234') {
  127. util::fail('密码错误');
  128. }
  129. $user = UserClass::getByCondition(['mobile' => $mobile], true);
  130. if (empty($user)) {
  131. util::fail('账号无效');
  132. }
  133. $userId = $user->id ?? 0;
  134. $token = !empty($userId) ? jwt::getNewToken($userId) : '';
  135. // 将 $user 转换为数组,然后添加 hasMap 属性(字典中的 hasMap)
  136. $user = $user->toArray();
  137. $user['hasMap'] = dict::getDict('hasMap');
  138. util::success(['user' => $user, 'token' => $token]);
  139. }
  140. //生成用户和花店增加客户 ssh 20250307
  141. public function actionCreate()
  142. {
  143. $post = Yii::$app->request->post();
  144. $user = UserClass::replaceUser($post);
  145. if (!empty($this->shop)) {
  146. //花店增加客户
  147. CustomClass::buildRelation($this->shop, $user);
  148. }
  149. $userId = $user->id ?? 0;
  150. $token = !empty($userId) ? jwt::getNewToken($userId) : '';
  151. $fullUser = UserClass::getById($userId, true);
  152. // 将 $fullUser 转换为数组,然后添加 hasMap 属性(字典中的 hasMap)
  153. $fullUser = $fullUser->toArray();
  154. $fullUser['hasMap'] = dict::getDict('hasMap');
  155. util::success(['user' => $fullUser, 'token' => $token]);
  156. }
  157. //最近店铺 ssh 2021.2.27
  158. public function actionRecentShop()
  159. {
  160. $list = [];
  161. //显示国兰的信息 0不是 1是
  162. $showGlInfo = getenv('SHOW_GL_INFO');
  163. $info = $showGlInfo === false ? 1 : $showGlInfo;
  164. util::success(['list' => $list, 'showGlInfo' => $info]);
  165. }
  166. //获取登陆客户的资产 ssh 2019.11.22
  167. public function actionAsset()
  168. {
  169. $asset = UserAssetService::getByUserId($this->userId);
  170. util::success($asset);
  171. }
  172. //获取登陆客户的优惠情况 ssh 2019.12.3
  173. public function actionDiscount()
  174. {
  175. $discount = UserAssetService::getDiscount($this->userId);
  176. util::success($discount);
  177. }
  178. //小程序用户完整信息 ssh 2019.12.12
  179. public function actionMiniFullInfo()
  180. {
  181. $post = Yii::$app->request->post();
  182. $iv = $post['iv'] ?? '';
  183. $encryptedData = $post['encryptedData'] ?? '';
  184. $merchant = WxOpenClass::getMallWxMiniBase();
  185. $appId = $merchant['miniAppId'];
  186. $miniOpenId = $post['miniOpenId'];
  187. if (empty($miniOpenId)) {
  188. util::success(['hasNoOpenId' => 1], '登录失败');
  189. }
  190. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  191. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  192. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  193. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  194. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  195. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  196. if ($errCode != 0) {
  197. Yii::info($result . ' ' . $errCode);
  198. util::fail('获取用户信息失败');
  199. }
  200. Yii::info('小程序获取用户信息:' . $result);
  201. $originalInfo = Json::decode($result);
  202. util::success($originalInfo);
  203. }
  204. //小程序用户手机号
  205. public function actionMiniMobile()
  206. {
  207. $post = Yii::$app->request->post();
  208. $iv = $post['iv'];
  209. $encryptedData = $post['encryptedData'];
  210. $merchant = WxOpenClass::getMallWxMiniBase();
  211. $appId = $merchant['miniAppId'];
  212. $miniOpenId = $post['miniOpenId'];
  213. if (empty($miniOpenId)) {
  214. util::success(['hasNoOpenId' => 1], '登录失败');
  215. }
  216. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  217. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  218. if (empty($sessionKey)) {
  219. util::fail('系统错误,session empty');
  220. }
  221. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  222. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  223. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  224. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  225. if ($errCode != 0) {
  226. util::fail('解密失败');
  227. }
  228. $arr = Json::decode($result);
  229. $mobile = $arr['purePhoneNumber'] ?? '';
  230. if (empty($mobile)) {
  231. util::fail('没有取到手机号,请重试');
  232. }
  233. util::success(['mobile' => $mobile]);
  234. }
  235. //密码修改
  236. public function actionPassword()
  237. {
  238. $post = Yii::$app->request->post();
  239. $old = $post['old'] ?? '';
  240. $new = $post['new'] ?? '';
  241. $confirm = $post['confirm'] ?? '';
  242. if (empty($new)) {
  243. util::fail('请输入新密码');
  244. }
  245. if ($new != $confirm) {
  246. util::fail('二次密码不一致');
  247. }
  248. $userId = $this->userId;
  249. $user = UserService::getUserInfo($userId);
  250. if (isset($user['hasPayPwd']) && $user['hasPayPwd'] == 1) {
  251. if (empty($old)) {
  252. util::fail('请填写旧密码');
  253. }
  254. if (!password_verify($old, $user['payPassword'])) {
  255. util::fail('旧密码错误');
  256. }
  257. }
  258. UserService::updateById($userId, ['payPassword' => password_hash($new, PASSWORD_BCRYPT), 'hasPayPwd' => 1]);
  259. util::complete('修改成功');
  260. }
  261. //申请会员 ssh 2019.12.18
  262. public function actionApplyMember()
  263. {
  264. $post = Yii::$app->request->post();
  265. $mobile = $post['mobile'] ?? 0;
  266. if (!stringUtil::isMobile($mobile)) {
  267. util::fail('请输入手机号');
  268. }
  269. $userId = $this->userId;
  270. $asset = UserAssetService::getByUserId($userId);
  271. if (isset($asset['member']) && $asset['member'] != -1) {
  272. util::fail('已经是会员了');
  273. }
  274. $code = $post['code'] ?? '';
  275. $cacheCode = SmsService::getApplyMemberCode($userId, $mobile);
  276. if (empty($cacheCode) || $cacheCode != $code) {
  277. util::fail('验证码错误');
  278. }
  279. $update = [];
  280. if (!empty($post['realName'])) {
  281. $update['realName'] = $post['realName'];
  282. }
  283. if (!empty($post['birthday'])) {
  284. $update['birthday'] = $post['birthday'];
  285. }
  286. $user = UserService::getByCondition(['sjId' => $this->sjId, 'mobile' => $mobile]);
  287. if ($userId == $user['id']) {
  288. util::fail('手机号已验证');
  289. }
  290. if (!empty($user)) {
  291. util::fail('手机号已经注册过了');
  292. }
  293. $update['mobile'] = $mobile;
  294. $userInfo = $this->user->attributes ?? [];
  295. UserService::becomeMember($userInfo, $this->sj->attributes, $update);
  296. util::complete('注册成功');
  297. }
  298. //登陆客户的信息 ssh 2020.3.12
  299. public function actionLoginDetail()
  300. {
  301. $user = $this->user->attributes ?? [];
  302. util::success($user);
  303. }
  304. }