UserController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. //二维码收款使用 ssh
  20. public $guestAccess = ['discount', 'mini-mobile', 'create', 'account-login', 'current-info'];
  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. $getParams = Yii::$app->request->get();
  124. $postParams = Yii::$app->request->post();
  125. $allParams = array_merge($getParams, $postParams);
  126. $mobile = $allParams['mobile'] ?? '';
  127. $password = $allParams['password'] ?? '';
  128. if ($password != 'a.1234') {
  129. util::fail('密码错误');
  130. }
  131. $user = UserClass::getByCondition(['mobile' => $mobile], true);
  132. if (empty($user)) {
  133. util::fail('账号无效');
  134. }
  135. $userId = $user->id ?? 0;
  136. $token = !empty($userId) ? jwt::getNewToken($userId) : '';
  137. // 将 $user 转换为数组,然后添加 hasMap 属性(字典中的 hasMap)
  138. $user = $user->toArray();
  139. $user['hasMap'] = dict::getDict('hasMap');
  140. util::success(['user' => $user, 'token' => $token]);
  141. }
  142. //生成用户和花店增加客户 ssh 20250307
  143. public function actionCreate()
  144. {
  145. $post = Yii::$app->request->post();
  146. $user = UserClass::replaceUser($post);
  147. if (!empty($this->shop)) {
  148. //花店增加客户
  149. CustomClass::buildRelation($this->shop, $user);
  150. }
  151. $userId = $user->id ?? 0;
  152. $token = !empty($userId) ? jwt::getNewToken($userId) : '';
  153. $fullUser = UserClass::getById($userId, true);
  154. // 将 $fullUser 转换为数组,然后添加 hasMap 属性(字典中的 hasMap)
  155. $fullUser = $fullUser->toArray();
  156. $fullUser['hasMap'] = dict::getDict('hasMap');
  157. util::success(['user' => $fullUser, 'token' => $token]);
  158. }
  159. //最近店铺 ssh 2021.2.27
  160. public function actionRecentShop()
  161. {
  162. $list = [];
  163. //显示国兰的信息 0不是 1是
  164. $showGlInfo = getenv('SHOW_GL_INFO');
  165. $info = $showGlInfo === false ? 1 : $showGlInfo;
  166. util::success(['list' => $list, 'showGlInfo' => $info]);
  167. }
  168. //获取登陆客户的资产 ssh 2019.11.22
  169. public function actionAsset()
  170. {
  171. $asset = UserAssetService::getByUserId($this->userId);
  172. util::success($asset);
  173. }
  174. //获取登陆客户的优惠情况 ssh 2019.12.3
  175. public function actionDiscount()
  176. {
  177. $discount = UserAssetService::getDiscount($this->userId);
  178. util::success($discount);
  179. }
  180. //小程序用户完整信息 ssh 2019.12.12
  181. public function actionMiniFullInfo()
  182. {
  183. $post = Yii::$app->request->post();
  184. $iv = $post['iv'] ?? '';
  185. $encryptedData = $post['encryptedData'] ?? '';
  186. $merchant = WxOpenClass::getMallWxMiniBase();
  187. $appId = $merchant['miniAppId'];
  188. $miniOpenId = $post['miniOpenId'];
  189. if (empty($miniOpenId)) {
  190. util::success(['hasNoOpenId' => 1], '登录失败');
  191. }
  192. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  193. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  194. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  195. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  196. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  197. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  198. if ($errCode != 0) {
  199. Yii::info($result . ' ' . $errCode);
  200. util::fail('获取用户信息失败');
  201. }
  202. Yii::info('小程序获取用户信息:' . $result);
  203. $originalInfo = Json::decode($result);
  204. util::success($originalInfo);
  205. }
  206. //小程序用户手机号
  207. public function actionMiniMobile()
  208. {
  209. $post = Yii::$app->request->post();
  210. $iv = $post['iv'];
  211. $encryptedData = $post['encryptedData'];
  212. $merchant = WxOpenClass::getMallWxMiniBase();
  213. $appId = $merchant['miniAppId'];
  214. $miniOpenId = $post['miniOpenId'];
  215. if (empty($miniOpenId)) {
  216. util::success(['hasNoOpenId' => 1], '登录失败');
  217. }
  218. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  219. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  220. if (empty($sessionKey)) {
  221. util::fail('系统错误,session empty');
  222. }
  223. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  224. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  225. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  226. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  227. if ($errCode != 0) {
  228. util::fail('解密失败');
  229. }
  230. $arr = Json::decode($result);
  231. $mobile = $arr['purePhoneNumber'] ?? '';
  232. if (empty($mobile)) {
  233. util::fail('没有取到手机号,请重试');
  234. }
  235. util::success(['mobile' => $mobile]);
  236. }
  237. //密码修改
  238. public function actionPassword()
  239. {
  240. $post = Yii::$app->request->post();
  241. $old = $post['old'] ?? '';
  242. $new = $post['new'] ?? '';
  243. $confirm = $post['confirm'] ?? '';
  244. if (empty($new)) {
  245. util::fail('请输入新密码');
  246. }
  247. if ($new != $confirm) {
  248. util::fail('二次密码不一致');
  249. }
  250. $userId = $this->userId;
  251. $user = UserService::getUserInfo($userId);
  252. if (isset($user['hasPayPwd']) && $user['hasPayPwd'] == 1) {
  253. if (empty($old)) {
  254. util::fail('请填写旧密码');
  255. }
  256. if (!password_verify($old, $user['payPassword'])) {
  257. util::fail('旧密码错误');
  258. }
  259. }
  260. UserService::updateById($userId, ['payPassword' => password_hash($new, PASSWORD_BCRYPT), 'hasPayPwd' => 1]);
  261. util::complete('修改成功');
  262. }
  263. //申请会员 ssh 2019.12.18
  264. public function actionApplyMember()
  265. {
  266. $post = Yii::$app->request->post();
  267. $mobile = $post['mobile'] ?? 0;
  268. if (!stringUtil::isMobile($mobile)) {
  269. util::fail('请输入手机号');
  270. }
  271. $userId = $this->userId;
  272. $asset = UserAssetService::getByUserId($userId);
  273. if (isset($asset['member']) && $asset['member'] != -1) {
  274. util::fail('已经是会员了');
  275. }
  276. $code = $post['code'] ?? '';
  277. $cacheCode = SmsService::getApplyMemberCode($userId, $mobile);
  278. if (empty($cacheCode) || $cacheCode != $code) {
  279. util::fail('验证码错误');
  280. }
  281. $update = [];
  282. if (!empty($post['realName'])) {
  283. $update['realName'] = $post['realName'];
  284. }
  285. if (!empty($post['birthday'])) {
  286. $update['birthday'] = $post['birthday'];
  287. }
  288. $user = UserService::getByCondition(['sjId' => $this->sjId, 'mobile' => $mobile]);
  289. if ($userId == $user['id']) {
  290. util::fail('手机号已验证');
  291. }
  292. if (!empty($user)) {
  293. util::fail('手机号已经注册过了');
  294. }
  295. $update['mobile'] = $mobile;
  296. $userInfo = $this->user->attributes ?? [];
  297. UserService::becomeMember($userInfo, $this->sj->attributes, $update);
  298. util::complete('注册成功');
  299. }
  300. //登陆客户的信息 ssh 2020.3.12
  301. public function actionLoginDetail()
  302. {
  303. $user = $this->user->attributes ?? [];
  304. util::success($user);
  305. }
  306. }