ConfigController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leo
  5. * Date: 2018/2/24
  6. * Time: 下午12:48
  7. */
  8. namespace frontendApi\modules\v1\controllers;
  9. use common\helpers\Cache;
  10. use common\helpers\Form;
  11. use common\helpers\snowflake\SnowFake;
  12. use common\models\forms\ReceiveAddressForm;
  13. use common\models\forms\UserConfigForm;
  14. use common\models\ReceiveAddress;
  15. use common\models\Region;
  16. use common\models\UserInfo;
  17. use yii\base\Exception;
  18. class ConfigController extends BaseController
  19. {
  20. public $modelClass = UserInfo::class;
  21. /**
  22. * 会员的相关配置
  23. * @return mixed
  24. * @throws \yii\web\HttpException
  25. */
  26. public function actionBase(){
  27. /**/
  28. $data = UserInfo::find()->select('IS_AUTO_WITHDRAW,ALLOW_RECONSUME_SMS')->where('USER_ID=:USER_ID', [':USER_ID'=>\Yii::$app->user->id])->asArray()->one();
  29. foreach($data as $key=>$value){
  30. if($key == 'IS_AUTO_WITHDRAW'){
  31. $data[$key] = boolval($value);
  32. }
  33. if($key == 'ALLOW_RECONSUME_SMS'){
  34. $data[$key] = boolval($value);
  35. }
  36. }
  37. $data['smsFee'] = Cache::getSystemConfig()['smsFee']['VALUE'];
  38. return static::notice($data);
  39. }
  40. /**
  41. * 修改自动提现设置
  42. * @return mixed
  43. * @throws \yii\db\Exception
  44. * @throws \yii\web\HttpException
  45. */
  46. public function actionAutoWithdraw(){
  47. if(\Yii::$app->request->isPost){
  48. $formModel = new UserConfigForm();
  49. $formModel->scenario = 'autoWithdraw';
  50. if($formModel->load(\Yii::$app->request->post(), '') && $formModel->autoWithdraw()){
  51. return static::notice('开启关闭自动提现成功');
  52. } else {
  53. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  54. }
  55. }
  56. }
  57. /**
  58. * 修改复销短信通知设置
  59. * @return mixed
  60. * @throws \yii\base\Exception
  61. * @throws \yii\db\Exception
  62. * @throws \yii\web\HttpException
  63. */
  64. public function actionAllowReconsumeSms(){
  65. if(\Yii::$app->request->isPost){
  66. $formModel = new UserConfigForm();
  67. $formModel->scenario = 'allowReconsumeSms';
  68. if($formModel->load(\Yii::$app->request->post(), '') && $formModel->allowReconsumeSms()){
  69. return static::notice('开启关闭复销短信提醒成功');
  70. } else {
  71. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  72. }
  73. }
  74. }
  75. /**
  76. * 收货地址列表
  77. * @return mixed
  78. * @throws \yii\web\HttpException
  79. */
  80. public function actionReceiveAddressList() {
  81. $condition = ' AND USER_ID=:USER_ID';
  82. $params[':USER_ID'] = \Yii::$app->user->id;
  83. $data = ReceiveAddress::lists($condition, $params, [
  84. 'SELECT' => 'ID,CONSIGNEE,MOBILE,PROVINCE,LGA_NAME,CITY_NAME,ADDRESS,IS_DEFAULT',
  85. 'orderBy' => 'IS_DEFAULT DESC,CREATED_AT DESC',
  86. 'useSlaves' => true,
  87. ]);
  88. if($data['list']){
  89. foreach($data['list'] as $key=>$row){
  90. $data['list'][$key]['PROVINCE_NAME'] = Region::getCnName($row['PROVINCE']);
  91. // $data['list'][$key]['CITY_NAME'] = Region::getCnName($row['CITY']);
  92. // $data['list'][$key]['COUNTY_NAME'] = Region::getCnName($row['COUNTY']);
  93. }
  94. }
  95. // print_r($data);exit;
  96. return static::notice($data);
  97. }
  98. /**
  99. * 获取一条收货地址
  100. * @return mixed
  101. * @throws \yii\web\HttpException
  102. */
  103. public function actionReceiveAddressOne() {
  104. $data = ReceiveAddress::findOneAsArray('USER_ID=:USER_ID AND ID=:ID', [':USER_ID'=>\Yii::$app->user->id, ':ID'=>\Yii::$app->request->get('id')]);
  105. return static::notice($data);
  106. }
  107. /**
  108. * 添加收货地址
  109. * @return mixed
  110. * @throws \yii\web\HttpException
  111. */
  112. public function actionReceiveAddressAdd() {
  113. Region::updateToCache();
  114. if(\Yii::$app->request->isPost) {
  115. return parent::edit(ReceiveAddressForm::class, '添加收货地址成功', 'userAdd', ['edit']);
  116. }
  117. return static::notice('非法访问', 400);
  118. }
  119. /**
  120. * 编辑收货地址
  121. * @return mixed
  122. * @throws \yii\web\HttpException
  123. */
  124. public function actionReceiveAddressEdit() {
  125. if(\Yii::$app->request->isPost) {
  126. return parent::edit(ReceiveAddressForm::class, '添加收货地址成功', 'userEdit', ['edit']);
  127. }
  128. return static::notice('非法访问', 400);
  129. }
  130. /**
  131. * 设置取消默认收货地址
  132. * @return mixed
  133. * @throws \yii\web\HttpException
  134. */
  135. public function actionReceiveAddressDefault() {
  136. if(\Yii::$app->request->isPost) {
  137. return parent::edit(ReceiveAddressForm::class, '添加收货地址成功', 'userIsDefault', ['edit']);
  138. }
  139. return static::notice('非法访问', 400);
  140. }
  141. /**
  142. * 删除
  143. * @return mixed
  144. * @throws \yii\db\Exception
  145. * @throws \yii\web\HttpException
  146. */
  147. public function actionReceiveAddressDelete() {
  148. if(\Yii::$app->request->isPost) {
  149. return parent::delete(ReceiveAddress::class, null, function(){
  150. // 如果没有默认地址的话,就在设置一个默认地址
  151. if(!ReceiveAddress::find()->where('USER_ID=:USER_ID AND IS_DEFAULT=1', [':USER_ID'=>\Yii::$app->user->id])->exists()){
  152. $model = ReceiveAddress::find()->where('USER_ID=:USER_ID', [':USER_ID'=>\Yii::$app->user->id])->one();
  153. $model->IS_DEFAULT = 1;
  154. if (!$model->save()){
  155. throw new Exception('更新默认地址失败');
  156. }
  157. }
  158. } , true);
  159. }
  160. return static::notice('非法访问', 400);
  161. }
  162. }