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