UserController.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. namespace App\Http\Controllers\QuickApp\User;
  3. use App\Http\Controllers\QuickApp\BaseController;
  4. use App\Http\Controllers\Wap\User\Transformers\SignRecordTransformer;
  5. use App\Libs\AliSMS;
  6. use App\Modules\Subscribe\Services\YearOrderService;
  7. use App\Modules\User\Services\QappUserService;
  8. use App\Modules\User\Services\ReadRecordService;
  9. use App\Modules\User\Services\UserSignService;
  10. use Illuminate\Http\Request;
  11. use Redis;
  12. class UserController extends BaseController
  13. {
  14. /**
  15. * @apiDefine User 用户
  16. */
  17. /**
  18. * @apiVersion 1.0.0
  19. * @apiDescription 获取用户信息
  20. * @api {GET} userinfo 获取用户信息
  21. * @apiHeader {String} [Authorization] token
  22. * @apiGroup User
  23. * @apiName index
  24. * @apiSuccess {Number} id 用户ID.
  25. * @apiSuccess {String} openid 微信openid.
  26. * @apiSuccess {String} unionid 微信unionid.
  27. * @apiSuccess {Number} distribution_channel_id 分销渠道ID.
  28. * @apiSuccess {String} province 省份.
  29. * @apiSuccess {String} city 城市.
  30. * @apiSuccess {String} country 国家.
  31. * @apiSuccess {String} headimgurl 头像地址.
  32. * @apiSuccess {Number} send_order_id 派单ID.
  33. * @apiSuccess {Number=0,1} sex 性别.
  34. * @apiSuccess {String} balance 书币余额.
  35. * @apiSuccess {Int} is_vip 是否vip
  36. * @apiSuccess {String} vip_days 364天.
  37. * @apiSuccess {String} phone 手机号.
  38. * @apiSuccessExample {json} Success-Response:
  39. *
  40. * {
  41. * "code": 0,
  42. * "msg": "",
  43. * "data": {
  44. * "id": 56,
  45. * "openid": "sdfs34ssdfdsf",
  46. * "unionid": "SDFSD3343S",
  47. * "distribution_channel_id": 1212,
  48. * "province": "浙江省",
  49. * "city": "杭州",
  50. * "country": "中国",
  51. * "headimgurl": "http://..",
  52. * "send_order_id": 323,
  53. * "balance": 8956,
  54. * "register_time": "2017-12-12 12:12:12",
  55. * "phone": "12312435",
  56. * }
  57. * }
  58. */
  59. public function index()
  60. {
  61. $data = $this->user_info;
  62. if (!$data->head_img) {
  63. $data->head_img = 'https://yueduyun.oss-cn-hangzhou.aliyuncs.com/xiaochengxu/img/defaulthead.png';
  64. }
  65. $data['is_vip'] = 0;
  66. $data['vip_days'] = 0;
  67. $data['phone'] = $this->phone;
  68. $year_record = YearOrderService::getRecord($this->uid);
  69. if ($year_record) {
  70. $data['is_vip'] = 1;
  71. $time = strtotime($year_record['end_time']) - time();
  72. if ($time >= 86400) {
  73. $data['vip_days'] = floor($time / 86400) . '天';
  74. } elseif ($time > 3600) {
  75. $data['vip_days'] = floor($time / 3600) . '小时';
  76. } elseif ($time > 60) {
  77. $data['vip_days'] = floor($time / 60) . '分钟';
  78. } else {
  79. $data['vip_days'] = $time . '秒';
  80. }
  81. }
  82. return response()->success($data);
  83. }
  84. /**
  85. * @apiVersion 1.0.0
  86. * @apiDescription 用户签到
  87. * @api {GET} sign 用户签到
  88. * @apiHeader {String} [Authorization] token
  89. * @apiGroup User
  90. * @apiName sign
  91. * @apiSuccess {Double} fee 签到奖励
  92. * @apiSuccess {Number} days 签到天数
  93. * @apiSuccessExample {json} Success-Response:
  94. *
  95. * {
  96. * "code": 0,
  97. * "msg": "",
  98. * "data": {
  99. * "fee": 30,
  100. * "days": 1
  101. * }
  102. * }
  103. */
  104. public function sign()
  105. {
  106. $result = UserSignService::signV2($this->uid, date('Y-m-d'));
  107. if ($result) {
  108. return response()->success($result);
  109. }
  110. return response()->error('QAPP_SYS_ERROR');
  111. }
  112. /**
  113. * @apiVersion 1.0.0
  114. * @apiDescription 发送验证码
  115. * @api {POST} user/sendCode 发送验证码
  116. * @apiHeader {String} [Authorization] token
  117. * @apiGroup User
  118. * @apiName sendCode
  119. * @apiParam {String} phone 手机号
  120. * @apiSuccessExample {json} Success-Response:
  121. *
  122. * {
  123. * "code": 0,
  124. * "msg": "",
  125. * "data": {}
  126. * }
  127. */
  128. public function sendCode(Request $request)
  129. {
  130. $phone = $request->post('phone');
  131. $code = random_int(1000, 9999);
  132. Redis::setex('quser_code:' . $phone, 120, $code);
  133. AliSMS::send($phone, 'quickapp_user_bind_phone', ['code' => $code]);
  134. return response()->success();
  135. }
  136. /**
  137. * @apiVersion 1.0.0
  138. * @apiDescription 绑定手机号
  139. * @api {POST} user/bindPhone 绑定手机号
  140. * @apiHeader {String} [Authorization] token
  141. * @apiGroup User
  142. * @apiName bindPhone
  143. * @apiParam {String} phone 手机号
  144. * @apiParam {String} code 验证码
  145. * @apiSuccessExample {json} Success-Response:
  146. *
  147. * {
  148. * "code": 0,
  149. * "msg": "",
  150. * "data": {}
  151. * }
  152. */
  153. public function bindPhone(Request $request)
  154. {
  155. $code = $request->post('code');
  156. $phone = $request->post('phone');
  157. $old = Redis::get('quser_code:' . $phone);
  158. if ($old && $old == $code) {
  159. try {
  160. Redis::del('quser_code:' . $phone);
  161. $result = QappUserService::bindPhoneStatic($this->uid, $phone);
  162. if (!$result) {
  163. return response()->error('WAP_BIND_PHONE_EXIST');
  164. } else {
  165. return response()->success();
  166. }
  167. } catch (\Exception $e) {
  168. return response()->error();
  169. }
  170. } else {
  171. return response()->error('WAP_SEND_CODE_ERROR');
  172. }
  173. }
  174. /**
  175. * @apiVersion 1.0.0
  176. * @apiDescription 用户签到记录
  177. * @api {GET} user/sign_record 用户签到记录
  178. * @apiGroup User
  179. * @apiName signRecord
  180. * @apiParam {String} date 查询日期
  181. * @apiSuccess {int} code 状态码
  182. * @apiSuccess {String} msg 信息
  183. * @apiSuccess {object} data 结果集
  184. * @apiSuccess {sign_status} data.sign_status .
  185. * @apiSuccess {sign_result} data.sign_result .
  186. * @apiSuccess {sign_today} data.sign_today .
  187. * @apiParam {page} page
  188. * @apiSuccessExample {json} Success-Response:
  189. *
  190. * {
  191. * code: 0,
  192. * msg: "",
  193. * data: {
  194. * "sign_status": true,
  195. * "sign_result": {
  196. * "list": [
  197. * {
  198. * "reward": 30,
  199. * "sign_time": "2019-11-01 14:20:30"
  200. * }
  201. * ],
  202. * "meta": {
  203. * "total": 1,
  204. * "per_page": 15,
  205. * "current_page": 1,
  206. * "last_page": 1,
  207. * "next_page_url": "",
  208. * "prev_page_url": ""
  209. * }
  210. * },
  211. * "sign_today": {
  212. * "uid": 162261523,
  213. * "price": 50,
  214. * "day": "2019-11-01",
  215. * "sign_time": "2019-11-01 09:04:43",
  216. * "created_at": "2019-11-01 09:04:43",
  217. * "updated_at": "2019-11-01 09:04:43",
  218. * "reward": 50
  219. * }
  220. * }
  221. */
  222. public function signRecord(Request $request)
  223. {
  224. $month = $request->get('date', date('Y-m-01'));
  225. $sign_result = paginationTransform(new SignRecordTransformer(), UserSignService::getUserSignRecord($this->uid, $month));
  226. $sign_status = UserSignService::isSign($this->uid);
  227. $sign_today = [];
  228. if ($sign_status) {
  229. $sign_today = ReadRecordService::getByField($this->uid, 'sign_info');
  230. if ($sign_today) $sign_today = json_decode($sign_today, 1);
  231. isset($sign_today['sign_time']) && $sign_today['sign_time'] = date('Y-m-d H:i:s', $sign_today['sign_time']);
  232. isset($sign_today['sign_at']) && $sign_today['sign_time'] = $sign_today['sign_at'];
  233. isset($sign_today['price']) && $sign_today['reward'] = $sign_today['price'];
  234. }
  235. $result = [
  236. 'sign_status' => $sign_status,
  237. 'sign_result' => $sign_result,
  238. 'sign_today' => $sign_today
  239. ];
  240. return response()->success($result);
  241. }
  242. }