ConfigController.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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\BaReceiveAddress;
  13. use common\models\forms\BaReceiveAddressForm;
  14. use common\models\forms\ReceiveAddressForm;
  15. use common\models\forms\UserConfigForm;
  16. use common\models\ReceiveAddress;
  17. use common\models\Region;
  18. use common\models\UserInfo;
  19. use Yii;
  20. use yii\base\Exception;
  21. class ConfigController extends BaseController
  22. {
  23. public $modelClass = UserInfo::class;
  24. /**
  25. * 会员的相关配置
  26. * @return mixed
  27. * @throws \yii\web\HttpException
  28. */
  29. public function actionBase(){
  30. /**/
  31. $data = UserInfo::find()->select('IS_AUTO_WITHDRAW,ALLOW_RECONSUME_SMS')->where('USER_ID=:USER_ID', [':USER_ID'=>\Yii::$app->user->id])->asArray()->one();
  32. foreach($data as $key=>$value){
  33. if($key == 'IS_AUTO_WITHDRAW'){
  34. $data[$key] = boolval($value);
  35. }
  36. if($key == 'ALLOW_RECONSUME_SMS'){
  37. $data[$key] = boolval($value);
  38. }
  39. }
  40. $data['smsFee'] = Cache::getSystemConfig()['smsFee']['VALUE'];
  41. return static::notice($data);
  42. }
  43. /**
  44. * 修改自动提现设置
  45. * @return mixed
  46. * @throws \yii\db\Exception
  47. * @throws \yii\web\HttpException
  48. */
  49. public function actionAutoWithdraw(){
  50. if(\Yii::$app->request->isPost){
  51. $formModel = new UserConfigForm();
  52. $formModel->scenario = 'autoWithdraw';
  53. if($formModel->load(\Yii::$app->request->post(), '') && $formModel->autoWithdraw()){
  54. return static::notice(Yii::t('app', 'autoWithdrawHasBeenClosed'));
  55. } else {
  56. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  57. }
  58. }
  59. }
  60. /**
  61. * 修改复销短信通知设置
  62. * @return mixed
  63. * @throws \yii\base\Exception
  64. * @throws \yii\db\Exception
  65. * @throws \yii\web\HttpException
  66. */
  67. public function actionAllowReconsumeSms(){
  68. if(\Yii::$app->request->isPost){
  69. $formModel = new UserConfigForm();
  70. $formModel->scenario = 'allowReconsumeSms';
  71. if($formModel->load(\Yii::$app->request->post(), '') && $formModel->allowReconsumeSms()){
  72. return static::notice(Yii::t('app', 'closeMessageSendSuccessfully'));
  73. } else {
  74. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  75. }
  76. }
  77. }
  78. /**
  79. * 收货地址列表
  80. * @return mixed
  81. * @throws \yii\web\HttpException
  82. */
  83. public function actionReceiveAddressList() {
  84. $condition = ' AND USER_ID=:USER_ID';
  85. $params[':USER_ID'] = \Yii::$app->user->id;
  86. $data = ReceiveAddress::lists($condition, $params, [
  87. 'SELECT' => 'ID,CONSIGNEE,MOBILE,PROVINCE,LGA_NAME,CITY_NAME,ADDRESS,IS_DEFAULT',
  88. 'orderBy' => 'IS_DEFAULT DESC,CREATED_AT DESC',
  89. 'useSlaves' => true,
  90. ]);
  91. if($data['list']){
  92. foreach($data['list'] as $key=>$row){
  93. $data['list'][$key]['PROVINCE_NAME'] = Region::getCnName($row['PROVINCE']);
  94. // $data['list'][$key]['CITY_NAME'] = Region::getCnName($row['CITY']);
  95. // $data['list'][$key]['COUNTY_NAME'] = Region::getCnName($row['COUNTY']);
  96. }
  97. }
  98. // print_r($data);exit;
  99. return static::notice($data);
  100. }
  101. /**
  102. * 获取一条收货地址
  103. * @return mixed
  104. * @throws \yii\web\HttpException
  105. */
  106. public function actionReceiveAddressOne() {
  107. $data = ReceiveAddress::findOneAsArray('USER_ID=:USER_ID AND ID=:ID', [':USER_ID'=>\Yii::$app->user->id, ':ID'=>\Yii::$app->request->get('id')]);
  108. return static::notice($data);
  109. }
  110. /**
  111. * 添加收货地址
  112. * @return mixed
  113. * @throws \yii\web\HttpException
  114. */
  115. public function actionReceiveAddressAdd() {
  116. Region::updateToCache();
  117. if(\Yii::$app->request->isPost) {
  118. return parent::edit(ReceiveAddressForm::class, Yii::t('app', 'successfully'), 'userAdd', ['edit']);
  119. }
  120. return static::notice(Yii::t('app', 'illegalRequest'), 400);
  121. }
  122. /**
  123. * 编辑收货地址
  124. * @return mixed
  125. * @throws \yii\web\HttpException
  126. */
  127. public function actionReceiveAddressEdit() {
  128. if(\Yii::$app->request->isPost) {
  129. return parent::edit(ReceiveAddressForm::class, Yii::t('app', 'successfully'), 'userEdit', ['edit']);
  130. }
  131. return static::notice(Yii::t('app', 'illegalRequest'), 400);
  132. }
  133. /**
  134. * 设置取消默认收货地址
  135. * @return mixed
  136. * @throws \yii\web\HttpException
  137. */
  138. public function actionReceiveAddressDefault() {
  139. if(\Yii::$app->request->isPost) {
  140. return parent::edit(ReceiveAddressForm::class, Yii::t('app', 'successfully'), 'userIsDefault', ['edit']);
  141. }
  142. return static::notice(Yii::t('app', 'illegalRequest'), 400);
  143. }
  144. /**
  145. * 删除
  146. * @return mixed
  147. * @throws \yii\db\Exception
  148. * @throws \yii\web\HttpException
  149. */
  150. public function actionReceiveAddressDelete() {
  151. if(\Yii::$app->request->isPost) {
  152. return parent::delete(ReceiveAddress::class, null, function(){
  153. // 如果没有默认地址的话,就在设置一个默认地址
  154. if(!ReceiveAddress::find()->where('USER_ID=:USER_ID AND IS_DEFAULT=1', [':USER_ID'=>\Yii::$app->user->id])->exists()){
  155. $model = ReceiveAddress::find()->where('USER_ID=:USER_ID', [':USER_ID'=>\Yii::$app->user->id])->one();
  156. $model->IS_DEFAULT = 1;
  157. if (!$model->save()){
  158. throw new Exception(Yii::t('app', 'changeDefaultAddressFailed'));
  159. }
  160. }
  161. } , true);
  162. }
  163. return static::notice(Yii::t('app', 'illegalRequest'), 400);
  164. }
  165. /**
  166. * 收货地址列表
  167. * @return mixed
  168. * @throws \yii\web\HttpException
  169. */
  170. public function actionBaReceiveAddressList() {
  171. $condition = ' AND USER_ID=:USER_ID';
  172. $params[':USER_ID'] = \Yii::$app->getUser()->getId();
  173. $data = BaReceiveAddress::lists($condition, $params, [
  174. 'SELECT' => 'ID,CONSIGNEE,MOBILE,PROVINCE,LGA_NAME,CITY_NAME,ADDRESS,IS_DEFAULT',
  175. 'orderBy' => 'IS_DEFAULT DESC,CREATED_AT DESC',
  176. 'useSlaves' => true,
  177. ]);
  178. if($data['list']){
  179. foreach($data['list'] as $key=>$row){
  180. $data['list'][$key]['PROVINCE_NAME'] = Region::getCnName($row['PROVINCE']);
  181. }
  182. }
  183. return static::notice($data);
  184. }
  185. /**
  186. * 获取一条收货地址
  187. * @return mixed
  188. * @throws \yii\web\HttpException
  189. */
  190. public function actionBaReceiveAddressOne() {
  191. $data = BaReceiveAddress::findOneAsArray(
  192. 'USER_ID=:USER_ID AND ID=:ID',
  193. [':USER_ID'=>\Yii::$app->getUser()->getId(), ':ID' => \Yii::$app->request->get('id')]
  194. );
  195. return static::notice($data);
  196. }
  197. /**
  198. * 添加收货地址
  199. * @return mixed
  200. * @throws \yii\web\HttpException
  201. */
  202. public function actionBaReceiveAddressAdd() {
  203. Region::updateToCache();
  204. if (\Yii::$app->request->isPost) {
  205. return parent::edit(BaReceiveAddressForm::class, Yii::t('app', 'successfully'), 'userAdd', ['edit']);
  206. }
  207. return static::notice(Yii::t('app', 'illegalRequest'), 400);
  208. }
  209. /**
  210. * 编辑收货地址
  211. * @return mixed
  212. * @throws \yii\web\HttpException
  213. */
  214. public function actionBaReceiveAddressEdit() {
  215. if(\Yii::$app->request->isPost) {
  216. return parent::edit(BaReceiveAddressForm::class, Yii::t('app', 'successfully'), 'userEdit', ['edit']);
  217. }
  218. return static::notice(Yii::t('app', 'illegalRequest'), 400);
  219. }
  220. /**
  221. * 设置取消默认收货地址
  222. * @return mixed
  223. * @throws \yii\web\HttpException
  224. */
  225. public function actionBaReceiveAddressDefault() {
  226. if(\Yii::$app->request->isPost) {
  227. return parent::edit(BaReceiveAddressForm::class, Yii::t('app', Yii::t('app', 'successfully')), 'userIsDefault', ['edit']);
  228. }
  229. return static::notice(Yii::t('app', 'illegalRequest'), 400);
  230. }
  231. /**
  232. * 删除
  233. * @return mixed
  234. * @throws \yii\db\Exception
  235. * @throws \yii\web\HttpException
  236. */
  237. public function actionBaReceiveAddressDelete() {
  238. if(\Yii::$app->request->isPost) {
  239. return parent::delete(BaReceiveAddress::class, null, function() {
  240. // 如果没有默认地址的话,就在设置一个默认地址
  241. if (!BaReceiveAddress::find()->where('USER_ID=:USER_ID AND IS_DEFAULT=1', [':USER_ID' => \Yii::$app->getUser()->getId()])->exists()) {
  242. $addressNumber = BaReceiveAddress::find()->where('USER_ID=:USER_ID', [':USER_ID' => \Yii::$app->getUser()->getId()])->count();
  243. if ($addressNumber >= 1) {
  244. $model = BaReceiveAddress::find()->where('USER_ID=:USER_ID', [':USER_ID' => \Yii::$app->getUser()->getId()])->one();
  245. $model->IS_DEFAULT = 1;
  246. if (!$model->save()) {
  247. throw new Exception(Yii::t('app', 'changeDefaultAddressFailed'));
  248. }
  249. }
  250. }
  251. } , true);
  252. }
  253. return static::notice(Yii::t('app', 'illegalRequest'), 400);
  254. }
  255. }