ConfigController.php 11 KB

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